from typing import List, Dict
import sqlite3


class Interaction:
    def __init__(self, item_id, user_id, interaction_type=0, info=None, extra_info=None):
        self.item_id = item_id
        self.user_id = user_id
        self.interaction_type = interaction_type
        self.info = info
        self.extra_info = extra_info


class FileLoader:
    def __init__(self, hyper_params):
        self.filename = hyper_params['database_path']
        #self.book = {}
        #self.reader = {}
        #self.id_to_book = {}
        self.intereaction_list: Dict[int, List[Interaction]] = {}
        self.book_cnt = 0
        self.reader_cnt = 0

        self.read_from_dataset_file()
        hyper_params['item_cnt'] = self.book_cnt
        hyper_params['user_cnt'] = self.reader_cnt

    def read_from_dataset_file(self):
        conn = sqlite3.connect(self.filename)
        c = conn.cursor()

        # view_great
        cursor1 = c.execute("select *  from view_great")
        for row in cursor1:
            reader_id = row[1]
            if reader_id not in self.intereaction_list:
                self.intereaction_list[reader_id] = []
            book_id = row[2]
            if row[3] == 1:  # user like
                if(row[4] is None or row[5] is None or row[6] is None or row[7] is None):
                    self.intereaction_list[reader_id].append(
                        Interaction(book_id, reader_id, 5))
                else:
                    extra_info = [row[4], row[5], row[6], row[7]]
                    self.intereaction_list[reader_id].append(
                        Interaction(book_id, reader_id, 5, None, extra_info))
            else:  # user dislike
                if(row[4] is None or row[5] is None or row[6] is None or row[7] is None):
                    self.intereaction_list[reader_id].append(
                        Interaction(book_id, reader_id, 6))
                else:
                    extra_info = [row[4], row[5], row[6], row[7]]
                    self.intereaction_list[reader_id].append(
                        Interaction(book_id, reader_id, 6, None, extra_info))

        # view_info
        cursor2 = c.execute("select *  from view_info")
        for row in cursor2:
            reader_id = row[1]
            if reader_id not in self.intereaction_list:
                self.intereaction_list[reader_id] = []
            book_id = row[2]
            if (row[3] is None or row[4] is None or row[5] is None or row[6] is None):
                self.intereaction_list[reader_id].append(
                    Interaction(book_id, reader_id, 1))
            else:
                extra_info = [row[3], row[4], row[5], row[6]]
                self.intereaction_list[reader_id].append(
                    Interaction(book_id, reader_id, 1, None, extra_info))

        # view_rate
        cursor3 = c.execute("select *  from view_rate")
        for row in cursor3:
            reader_id = row[1]
            if reader_id not in self.intereaction_list:
                self.intereaction_list[reader_id] = []
            book_id = row[2]
            score = row[3]
            if (row[4] is None or row[5] is None or row[6] is None or row[7] is None):
                self.intereaction_list[reader_id].append(
                    Interaction(book_id, reader_id, 4, score))
            else:
                extra_info = [row[4], row[5], row[6], row[7]]
                self.intereaction_list[reader_id].append(
                    Interaction(book_id, reader_id, 4, score, extra_info))

        # view_reader
        cursor4 = c.execute("select *  from view_reader")
        for row in cursor4:
            reader_id = row[1]
            if reader_id not in self.intereaction_list:
                self.intereaction_list[reader_id] = []
            book_id = row[2]
            if (row[3] is None or row[4] is None or row[5] is None or row[6] is None):
                self.intereaction_list[reader_id].append(
                    Interaction(book_id, reader_id, 0))
            else:
                extra_info = [row[3], row[4], row[5], row[6]]
                self.intereaction_list[reader_id].append(
                    Interaction(book_id, reader_id, 0, None, extra_info))

        # view_speed
        cursor5 = c.execute("select *  from view_speed")
        for row in cursor5:
            reader_id = row[1]
            if reader_id not in self.intereaction_list:
                self.intereaction_list[reader_id] = []
            book_id = row[2]
            speed = row[3]
            if (row[4] is None or row[5] is None or row[6] is None or row[7] is None):
                self.intereaction_list[reader_id].append(
                    Interaction(book_id, reader_id, 3, speed))
            else:
                extra_info = [row[4], row[5], row[6], row[7]]
                self.intereaction_list[reader_id].append(
                    Interaction(book_id, reader_id, 3, speed, extra_info))

        # view_stay
        cursor6 = c.execute("select *  from view_stay")
        for row in cursor6:
            reader_id = row[1]
            if reader_id not in self.intereaction_list:
                self.intereaction_list[reader_id] = []
            book_id = row[2]
            stay_time = row[3]
            if (row[4] is None or row[5] is None or row[6] is None or row[7] is None):
                self.intereaction_list[reader_id].append(
                    Interaction(book_id, reader_id, 2, stay_time))
            else:
                extra_info = [row[4], row[5], row[6], row[7]]
                self.intereaction_list[reader_id].append(
                    Interaction(book_id, reader_id, 2, stay_time, extra_info))

        conn.close()

        # book_cnt
        conn = sqlite3.connect('database/book_data.db')
        c = conn.cursor()

        cursor = c.execute("select *  from book_info")
        row = cursor.fetchall()
        self.book_cnt = len(row)
        conn.close()

        # reader_cnt
        conn = sqlite3.connect('database/user_data.db')
        c = conn.cursor()

        cursor = c.execute("select *  from user_pwd")
        row = cursor.fetchall()
        self.reader_cnt = len(row)
        conn.close()

        print(f'Interact: {len(self.intereaction_list)}')


if __name__ == '__main__':
    hyper_params = {
        'database_path': '../database/interact_data.db'
    }

    file_loader = FileLoader(hyper_params=hyper_params)
    print(
        f'Interact: {len(file_loader.intereaction_list)}')
